//
//  BTEDeleteViewController.swift
//  BTE
//
//  Created by 张文 on 2025/8/16.
//

import UIKit

class BTEDeleteViewController: BTEBaseViewController {
    
    fileprivate let server = BTEMeToolNetServer()
    
    var model: BTEMEModel? {
        didSet {
            guard let m = model else {
                return
            }
            v.t1.text = m.notepaper?.aftermath
            v.t2.text = m.notepaper?.dis
            v.prilabe.text = m.notepaper?.bres
        }
    }

    var v: BTEDeleteView {
        return view as! BTEDeleteView
    }
    
    override func loadView() {
        view = BTEDeleteView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        updateNavigationTitle(BTELanguageManager.shared.localizedString(forKey:"Account cancellation"))
  
        v.loginButton.isUserInteractionEnabled = true
        let ta = UITapGestureRecognizer.init(target: self, action: #selector(dele))
        v.loginButton.addGestureRecognizer(ta)
        
        v.list.snp.remakeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationBar.snp.bottom)
        }
    }

    
    @objc fileprivate func dele(){
        let cli = v.pribut.isSelected
        if !cli {
            BTEHUD.shared.showError(BTELanguageManager.shared.localizedString(forKey: "Please select the agreement"), on: view)
            return
        }
        BTEHUD.shared.showLoader(on: view)
        server.earworms {[weak self] result in
            switch result {
            case .success(let success):
                
                NotificationCenter.default.post(name: .logout, object: nil)
                
                BTEHUD.shared.showSuccess(success?.squushy ?? "", on: (self?.view)!)
                UserDefaults.standard.set("", forKey: USER_TOKEN)
                self?.navigationController?.popToRootViewController(animated: false)
                
                if  let windowScene = UIApplication.shared.connectedScenes
                    .first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene,
                      let rootVC = windowScene.windows.first(where: { $0.isKeyWindow })?.rootViewController  as? UITabBarController {
                    rootVC.selectedIndex = 0
                    (rootVC as! BTEBaseTabBarController).basetabbar.la.image = UIImage.init(named: "gwerger432323232")
                    (rootVC as! BTEBaseTabBarController).basetabbar.ed.image = UIImage.init(named: "fwef32323232er23")
                }
                break
            case .failure(let failure):
                switch failure {
                case .Error(c: let b, m: let bd):
                    BTEHUD.shared.showError(bd, on: (self?.view)!)
                    break
                default:
                    break
                }
                break
            }
        }
    }
}
